set more off

use dataset, clear

gen B = .
gen UB = .
gen LB = .

* programs to store estimates (eststo doesn't work well with cgmboot)
capture program drop myest1
program define myest1
	args i bw cov
	mat bootresults = e(bootresults)
	svmat bootresults
	replace B = _b[Zz] if index == `i'
	_pctile bootresults6 if bootresults3 == 0, nq(1000)
	replace LB = `r(r25)' if index == `i'
	replace UB = `r(r975)' if index == `i'
	drop bootresults*
end
 
local Z pcteligible2013

su `Z', det
gen Z = `Z' >=r(p50)
replace Z = . if mi(`Z')
 
gen Zx = Z*x
gen Zz = Z*z
gen Zzx = Z*zx

keep if abs(x) < 100

*** do the regressions ***

local j = 1

gen index = _n
gen pre = .
gen post = .
gen outcome = ""

local nreps 500

foreach y in registration turnout {

	forvalues post = 2006(2)2016 {

		local post0 = `post' - 2
		
		forvalues pre = 2004(2)`post0' {

			cgmwildboot d`y'`post'`pre' z Z Zz x zx Zx Zzx , cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
			
			myest1 `j'
			
			replace pre = `pre' if index == `j'
			
			replace post = `post' if index == `j'
			
			replace outcome = "`y'" if index == `j'
			
			local j = `j' + 1
			
		
		}

	}

}

collapse B UB LB, by(pre post outcome)

keep if !mi(B)

sort outcome post pre
bys outcome post: gen index = _n
gen post2 = post 
replace post2 = post + .25*(index - 1)

foreach y in registration turnout {

	if "`y'" == "registration" local mytitle = "Registration"
	if "`y'" == "turnout" local mytitle = "Turnout"

	su B if post < 2013 & outcome == "`y'"
	local bpre = r(mean)
	su B if post > 2013 & outcome == "`y'"
	local bpost = r(mean)
	
	#delimit;

	gr tw 
		(sc B post2 if outcome == "`y'", col(black)) 
		(rspike UB LB post2 if outcome == "`y'", col(black))
		(scatteri `bpre' 2006 `bpre' 2013, recast(line) col(black) lpat(dash) lwid(medthick))
		(scatteri `bpost' 2013 `bpost' 2017, recast(line) col(black) lpat(dash) lwid(medthick))
		(sc UB post2 if outcome == "`y'", col(black) mlab(pre) msym(none) mlabpos(12) mlabsize(tiny))
		,
			xline(2013, lcol(black))
			xlab(2006(2)2016)
			ylab(, angle(horiz))
			plotregion(style(none))
			legend(off)
			name(g`y', replace)
			title(`mytitle')
			xtitle("")
			;

	#delimit cr

}

gr combine gregistration gturnout, rows(2) ysize(1.5) xsize(1)

gr export "_output/figureA10.pdf", replace
gr close
